package com.ftg.learn.chapter10.work03;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;
import java.util.function.Function;

/**
 * 学生信息
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/2/3 上午8:10
 */
@Data
@AllArgsConstructor
public class Student01 {
//    有一个学生数组，编写一个方法完成取出当前学生信息所有不重复的学生年龄

    public int abc(Student01 o){
        return this.getAge() - o.getAge();
    }

    String name;

    String sex;

    int age;

    public static void main(String[] args) {
        Student01[] stus = new Student01[3];
        stus[0] = new Student01("张三","男",18);
        stus[1] = new Student01("王二狗","男",18);
        stus[2] = new Student01("李春花","女",18);

        Function<Student01[],int[]> fn = x ->{
            //这个定长数组的长度是几 ....
            int[] as = new int[x.length];
            int index = 0;
            //x 就是Student01
            for (int i = 0; i < x.length; i++) {
                //当前这个学生的年龄
                //循环Student01取出里面的年龄 ，
                int age = x[i].getAge();
                //判断年龄是否存在as里面
                boolean ist = true;
                if(index ==0){
                    as[index++] = age;
                }else{
                    for(int j = 0 ;j < index;j++){
                        if(age == as[j]){
                            ist = false;
                            break;
                        }
                    }
                    if(ist){
                        as[index++] = age;
                    }
                }
            }
            return Arrays.copyOf(as,index);
        };

        int[] ages = fn.apply(stus);

        System.out.println(Arrays.toString(ages));

    }


}
